const PassportBiz = require('../../../../../comm/biz/passport_biz.js');
const pageHelper = require('../../../../../helper/page_helper.js');
const ProjectBiz = require('../../../biz/project_biz.js');
const RequestBiz = require('../../../../../comm/biz/request_biz.js');
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		isLoad: false
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		ProjectBiz.initPage(this);
		if (!pageHelper.getOptions(this, options)) return;
		let user = PassportBiz.getToken();
		this.setData({
			user: user,
			status: options.status,
			maxCnt: options.maxCnt,
			_params: { activityId: this.data.id, userId: user? user.id : '' },
			isLoad: true,
			fields: 'id,activityId,userId,addTime,userPic,showName,currentUser,vipLevel,'
		});
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {
	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	},

	url: async function (e) {
		pageHelper.url(e, this);
	},

	bindCommListCmpt: function (e) {
		pageHelper.commListListener(this, e);
		this.insertMyNameIfNotExists();
	},

  // 修改接龙报名人
  bindChangeName: function(e) {
    let index = e.currentTarget.dataset.index;
    let val = e.detail.value;
		let dataList = this.data.dataList;
		dataList.list[index].showName = val;
    this.setData({
      dataList: dataList
    });
	},
	
	bindAddTap: async function (e) {
		let dataList = this.data.dataList;
		let obj = {
			currentUser: true,
			showName: this.data.user.name,
			userPic: this.data.user.pic,
			addTime: this.currentTime(),
			new: true,
			activityId: this.data.id,
			userId: this.data.user.id,
			activityId: this.data.id,
			vipLevel: this.data.user.vipLevel,
		}
		dataList.list.push(obj);
		this.setData({
			dataList: dataList
		});
	},
	
	bindDeleteTap: async function (e) {
		wx.showLoading({ title: '删除报名信息' });
		let id = e.currentTarget.dataset.id;
		if (id) {
			let req = await RequestBiz.request('/api/ysjl/activity/deleteEnroll', {id: id});
			wx.hideLoading();
			if (req.data.ret != 0) {
				pageHelper.showErrToast(req.data.msg, 1000, async function () {
					wx.navigateBack();
				})
				return;
			}
		}
		let index = e.currentTarget.dataset.index;
		let dataList = this.data.dataList;
		dataList.list.splice(index, 1);
		this.setData({
			dataList: dataList
		});
		wx.hideLoading();
	},
	
	bindJoinTap: async function (e) {
		wx.showLoading({ title: '接龙中~' });
		let list = this.data.dataList.list;
		if (list.length < 1) {
			wx.hideLoading();
			wx.navigateBack();
			return;
		}

		let myList = [];
		for (let i = 0, len = list.length; i < len; i++) {
			if (list[i].currentUser) {
				myList.push(list[i]);
			}
		}
		if (myList.length < 1) {
			wx.hideLoading();
			wx.navigateBack();
			return;
		}

		if (myList.length > 6) {
			pageHelper.showErrToast('给你脸了是不', 2000, async function () {
				wx.hideLoading();
			});
			return;
		}

		let req = RequestBiz.request('/api/ysjl/activity/join', myList);
		req.then(res => {
			wx.hideLoading();
			let retData = res.data;
			if (retData.ret == 0) {
				pageHelper.showSuccToast('是兄弟就来打球', 2000, async function () {
					wx.navigateBack();
				});
			}
			else {
				pageHelper.showErrToast(retData.msg, 2000, async function () {
					wx.navigateBack();
				});
			}
		});
	},

	insertMyNameIfNotExists: function() {
		let dataList = this.data.dataList;
		for (let i = 0, len = dataList.list.length; i < len; i++) {
			if (dataList.list[i].currentUser) {
				return;
			}
		}

		this.bindAddTap(null);
	},

	currentTime: function() {
		let now = new Date(); // 获取当前时间
		let year = now.getFullYear(); // 获取当前年份
		let month = now.getMonth() + 1; // 获取当前月份，加“1”是因为月份是从0开始计算的，但实际月份是从1开始的
		let day = now.getDate(); // 获取当前日期
		let hours = now.getHours(); // 获取当前日期
		let minute = now.getMinutes(); // 获取当前日期
		let second = now.getSeconds(); // 获取当前日期
		return year + '-' + ('00' + month).substr(-2) + '-' + ('00' + day).substr(-2) + ' ' + hours + ':' + minute + ":" + second;
	}

})